﻿//7.24
//内容简介: 关于红心歌曲转换时的一系列转换问题

//转为显示红心以及相关设置
function ChangeToLikeState()
{
    console.log("切换到红心状态");
    $("#playerLove").css("background-position", "-400px 0");
    AddCookie("LikeCurrentSong", true, 0);
}

//转为显示非红心以及相关设置
function ChangeToUnlikeState()
{
    $("#playerLove").css("background-position", "0 0");
    AddCookie("LikeCurrentSong", false, 0);
}

//喜欢与不再喜欢操作
$("#playerLove").click(function () {
    console.log("点击喜欢btn");
    if (IsLogin()) {
        var userToken = GetCurrentUserId();
        var songId = GetCurrentSongId();
        if (LikeCurrentSong() == "true")
            DeleteLikeSong(userToken, songId);
        else
            AddLikeSong(userToken, songId);
    }
    else {
        //打开登录窗口
        alert("需要先登录");
    }
});

document.getElementById("playerLove").click = function () {
    if (IsLogin()) {
        var userToken = GetCurrentUserId();
        var songId = GetCurrentSongId();
        if (LikeCurrentSong() == "true")
            DeleteLikeSong(userToken, songId);
        else
            AddLikeSong(userToken, songId);
    }
    else {
        //打开登录窗口
        alert("需要先登录");
    }
}

//添加红心歌曲
function AddLikeSong(userId, songId) {
    $.ajax({
        url: "/Home/AddLike",
        data: {
            id: songId,
            UserToken: userId
        },
        success: function (data) {
            console.log(data);
            var result = $.parseJSON(data);
            if (result.Ok == true) {
                //转变红心样式
                ChangeToLikeState();
            }
            else {
                alert("添加失败" + result.Msg);
            }
        },
        error: function () {
            alert("网络错误, 刷新重试");
        }
    })
}

//点击红心发送不再喜欢的请求
function DeleteLikeSong() {
    var userToken = GetCurrentUserId();
    var songId = GetCurrentSongId();
    $.ajax({
        url: "/Home/DeleteLikeMusic",
        data: {
            id: userToken,
            SongId: songId
        },
        dataType: "json",
        success: function (data) {
            if (data.Ok == true) {
                ChangeToUnlikeState();
            }
            else {
                console.log(data.Msg);
            }
        },
        error: function () {
            alert("网络错误, 刷新重试");
        }
    })
}

//当前歌曲是喜欢歌曲
function LikeCurrentSong()
{
    var like = GetCookieByName("LikeCurrentSong");
    return like;
}